--
-- Author: yawei.zhu
-- Date: 2018-06-04 11:41:16
--
-- DailyRewardView
--
local IconRender = require("app.widget.renders.IconRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.DailyTaskReward

local rewardNum = 3 -- 奖励个数

function ClassRef:init()
end

function ClassRef:showData(data, isPrivilegeBox)
	local items
	self._isPrivilegeBox = isPrivilegeBox
	self._isLock = data.isLock
	if not isPrivilegeBox then
		self._boxIndex = data.boxIndex
		self._questType = data.questType
		items = data.itemList
	else	--特权宝箱
		dump(data)
		self._isPrivilege = data.isPrivilege	--是否是特权用户
		self._isGet = data.isGet
		items = UD:parseDropIDToBagItem(data.drop)
	end

	self:initView()
	self:refreshReward(items)
	self:setNodeEventEnabled(true)
end

function ClassRef:initView()
	local rootPanel = self._ui["Panel"]
	local lockMask = rootPanel:findChild("icon/mask")
	lockMask:setVisible(self._isLock)
	display.nodeRemoveForNotHit(rootPanel, function()
		self:closeView()
	end)

	local btnOk = rootPanel:getChildByName("Button_ok")
	if not self._isPrivilegeBox then
		btnOk:getChildByName("name"):setString(self._isLock and L("text_10200017") or L("tips_2000235"))
	else
		btnOk:getChildByName("name"):setString(self._isGet and L("privilegeboxunlock") or L("privilegeboxlocked"))
		btnOk:getChildByName("name"):setString(self._isPrivilege and L("privilegeboxlocked") or L("privilegeboxunpurchase"))
	end

	local function openPrivilegeBox(sender)
		if self._isPrivilege then
			if self._isGet and not self._isLock then
				local DailyModel = app:getInst("DailyModel")
				DailyModel:openSuperPrivilegeBox()
			end
		else
			local rewardModel = app:getInst("RewardModel")
			app:sendMsg("RewardController", "openView", rewardModel:getPrivilegeActivity())
		end
		self:closeView()
	end

	local function openDailyTaskBox(sender)
		if not self._isLock then
			if self._questType == 5 then
				TaskManager:C2S_GetDailyTaskRewardRequest(self._boxIndex)
			elseif self._questType == 6 then
				TaskManager:C2S_GetWeeklyTaskRewardRequest(self._boxIndex)
			end
		end
		self:closeView()
	end
	btnOk:addClickEventListener(self._isPrivilegeBox and openPrivilegeBox or openDailyTaskBox)

	self._ui.tlt:setString(L("task_award1"))
	self._ui.tips_1:setString( L("task_award2") )
	display.doTipBgLayout(self._ui.tips_1, self._ui.tips_img)
end

-- 刷新奖励部分
function ClassRef:refreshReward(itemLists)
	if not itemLists then return end
	local rootPanel = self._ui["Panel"]
	for i = 1, rewardNum do
		local uiItem = IconRender.create( rootPanel:getChildByName("icon_" .. i) )
		uiItem:setTipEnable( true )
		local dataTemp = nil
		if i <= #itemLists then
			dataTemp = itemLists[i]
		end
		uiItem:setData(dataTemp)
	end
end


return ClassRef
